package m2;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //创建消费者
        /*
            push-服务器主动推送消息
            pull-消费者先向服务器请求消息，服务器在发送
         */
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("consumer-group1");//对消费者进行分组
        //连接name server服务器
        c.setNamesrvAddr("192.168.64.141:9876");

        //指定从哪个主题，订阅什么标签的信息
        c.subscribe("Topic1","TagA || TagB || TagC");

        //设置监听器，处理信息

        c.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext ctx) {
                for(MessageExt msg : list){
                    System.out.println(new String(msg.getBody())+"-"+msg);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;//成功
                //return ConsumeConcurrentlyStatus.RECONSUME_LATER;//稍后重新消费
            }
        });

        //启动
        c.start();
        System.out.println("开始消费数据");
    }
}
